package com.zhilei;

import com.zhilei.intercept.JWTCurrentUserInterceptor;
import com.zhilei.intercept.SMSInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 这个配置类会将拦截器进行注册，实现webmvc的接口
 *
 * @author zhilei
 * @data 2024-09-04
 */
@Configuration
@Slf4j
public class InterceptorConfig implements WebMvcConfigurer {

    // 声明一个 bean 并返回 SMSInterceptor 实例，并在容器中注入
    @Bean
    public SMSInterceptor smsInterceptor() {
        return new SMSInterceptor();
    }

    // 声明一个 bean 并返回 JWTCurrentUserInterceptor 实例，并在容器中注入
    @Bean
    public JWTCurrentUserInterceptor jwtCurrentUserInterceptor() {
        return new JWTCurrentUserInterceptor();
    }

    // 配置需要拦截的控制器的接口是那些
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(smsInterceptor())
                // 拦截器拦截的路径，是发送短信验证码的接口
                .addPathPatterns("/passport/getSMSCode");

        registry.addInterceptor(jwtCurrentUserInterceptor())
                .addPathPatterns("/**");
    }
}
